/**
 *
 * Created by Administrator on 2015/6/1.
 */


var DB = require('./db');

function User(username, password, nicename, level, email) {
    this.username = username;
    this.password = password;
    this.nicename = nicename;
    this.level = level;
    this.email = email;

}

//添加用户
User.prototype.save = function (callback) {
    var _this = this;
    DB.connect(function (err, db) {
        if (err) {
            return callback(err, {});
        }

        var newUser = {
            username: _this.username,
            password: _this.password,
            nicename: _this.nicename,
            level: 'admin',
            email: _this.email
        };

        var user = db.collection('user');
        user.insertOne(newUser, function (err, result) {
            db.close();
            return callback(err, result);
        })
    })
}

//查找一个用户，类方法
User.findOne = function (queryOption, callback) {
    DB.connect(function (err, db) {
        if (err) {
            return callback(err, {});
        }

        var user = db.collection('user');
        user.findOne(queryOption, function (err, doc) {
            db.close();
            return callback(err, doc);
        })
    })
}

module.exports = User;
